package com.nit.generate;

import com.nit.tools.loop.NitLoopTools;
import net.andreinc.mockneat.utils.LoopsUtils;
import org.apache.commons.lang3.RandomUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 数字 生成器
 *
 * @Auth: 逆天开源-逍遥哥哥
 * @link: 1715656022@qq.com
 * @Date: 2023-07-11
 */
public class NitNumberGenerator {

    public static void main(String[] args) {
        NitLoopTools.loop(100, () -> {
            int tmp = NitNumberGenerator.generate();
            System.out.println("随机数字:" + tmp + "<br>");
        });
    }


    public static int generate() {
        return generateInt();
    }

    /**
     * 返回1个随机整数
     *
     * @return 1个随机整数
     */
    public static int generateInt() {
        return RandomUtils.nextInt();
    }

    public static int generateInt(Integer length) {
        StringBuffer minSb = new StringBuffer(1);
        LoopsUtils.loop(length - 1, () -> {
            minSb.append(0);
        });
        int min = Integer.parseInt(minSb.toString());
        StringBuffer maxSb = new StringBuffer(9);
        LoopsUtils.loop(length - 1, () -> {
            maxSb.append(9);
        });
        int max = Integer.parseInt(maxSb.toString());
        return generateInt(min, max);
    }

    /**
     * 返回1个随机整数
     *
     * @param startInclusive 开始(含)
     * @param endExclusive   结束(不含)
     * @return 1个随机整数
     */
    public static int generateInt(final int startInclusive, final int endExclusive) {
        return RandomUtils.nextInt(startInclusive, endExclusive);
    }


    /**
     * 返回1个随机长整数
     *
     * @return 1个随机长整数
     */
    public static long generateLong() {
        return RandomUtils.nextLong(Long.MIN_VALUE, Long.MAX_VALUE);
    }

    /**
     * 返回1个随机长整数
     *
     * @param startInclusive 开始(含)
     * @param endExclusive   结束(不含)
     * @return 1个随机长整数
     */
    public static long generateLong(final long startInclusive, final long endExclusive) {
        return RandomUtils.nextLong(startInclusive, endExclusive);
    }


    /**
     * 返回1个随机双精度数
     *
     * @return 1个随机双精度数
     */
    public static double generateDouble() {
        return RandomUtils.nextDouble(Double.MIN_VALUE, Double.MAX_VALUE);
    }

    /**
     * 返回1个随机双精度数
     *
     * @param startInclusive 开始(含)
     * @param endExclusive   结束(不含)
     * @return 1个随机双精度数
     */
    public static double generateDouble(final double startInclusive, final double endExclusive) {
        return RandomUtils.nextDouble(startInclusive, endExclusive);
    }

    /**
     * 随机百分比
     *
     * @return 1个随机百分比
     */
    public static BigDecimal generatePercent() {
        return BigDecimal.valueOf(RandomUtils.nextDouble(0.0001d, 0.9999d)).setScale(4, RoundingMode.HALF_UP);
    }
}
